<?php
namespace Addons\TimedTask\Controller\Api;
use Addons\QyWechat\Services\QyWechatApi;
use Models\BaseModel as Model;
use Addons\QyChatData\Helper;

class Balance extends \BaseController
{
	public function index(){
		$res = ['code'=>'ok'];
		$users = (new Model("user"))->where(["balance@!="=>""])->list_all();
		foreach ($users as $user){
			$lists = (new Model("user_recharge_record"))->where(["status"=>2,"openid"=>$user->id,"user_id"=>$user->id])->field("id,status,money")->list_all_array();
			if(!count($lists)) continue;
			$ids = array_column($lists,"id");
			$moneys = array_column($lists,"money");
			$user->balance = $user->balance - array_sum($moneys);
			if($user->save()){
				$external = (new Model('user_recharge_record'))->update(['id'=>$ids],['status'=>1]);
			}
		}
		return json_encode($res);
	}
}